package com.beermanager.activities;

import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import com.beermanager.R;
import com.doffman.dragarea.DragArea;
import com.doffman.dragarea.DragEvent;


/**
 * User: paulo
 * Date: 6/29/12
 * Time: 10:04 AM
 */
public class ManDraggable extends ImageView {
    private DragArea mDragArea;
    private Drawable mMenImage;

    public ManDraggable(Context context) {
        super(context);
        initDraggableMan();
    }

    public ManDraggable(Context context, AttributeSet attrs) {
        super(context, attrs);
        initDraggableMan();
    }

    public ManDraggable(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initDraggableMan();
    }

    private void initDraggableMan(){
        setBackgroundDrawable(getContext().getResources().getDrawable(R.drawable.man_icon));
        mMenImage = getBackground();
    }

    public void setDragArea(DragArea dragArea)
    {
        mDragArea = dragArea;
        this.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View view, MotionEvent event)
            {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    Bundle data = new Bundle();
                    data.putCharSequence("number", (String)getTag());
                    mDragArea.startDrag(data,
                            new DrawableDragShadowBuilder(
                                    ManDraggable.this,
                                    mMenImage,
                                    new Point((int)event.getX() - getPaddingLeft(),
                                            (int)event.getY() - getPaddingTop())));
                    return true;
                } else {
                    return false;
                }
            }
        });

        mDragArea.addDragListener(ManDraggable.this, new com.doffman.dragarea.OnDragListener()
        {
            @Override
            public void onDrag(View view, DragEvent dragEvent)
            {
                switch (dragEvent.getAction())
                {
                    case DragEvent.ACTION_DRAG_STARTED:
                        ManDraggable.this.setVisibility(INVISIBLE);
                        break;
                    case DragEvent.ACTION_DRAG_ENTERED:
                        break;
                    case DragEvent.ACTION_DRAG_EXITED:
                        break;
                    case DragEvent.ACTION_DROP:
                        break;
                    case DragEvent.ACTION_DRAG_ENDED:
                        ManDraggable.this.setVisibility(VISIBLE);
                        break;
                    default:
                        break;
                }
            }
        });
    }
}
